package com.mazaiting.flutterexample

import android.content.Intent
import android.os.Bundle

import io.flutter.app.FlutterActivity
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugins.GeneratedPluginRegistrant

class MainActivity: FlutterActivity() {
  /**
   * 分享文本
   */
  private var sharedText: String? = null
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    GeneratedPluginRegistrant.registerWith(this)
    
    val action = intent.action
    val type = intent.type
    
    if (Intent.ACTION_SEND == action && type != null) {
      if ("text/plain" == type) {
        handleSendText(intent)
      }
    }
    
    MethodChannel(flutterView, "app.channel.shared.data")
            .setMethodCallHandler { call, result ->
              if (call.method!!.contentEquals("getSharedText")){
                result.success(sharedText)
                sharedText = null
              }
            }
  }
  
  private fun handleSendText(intent: Intent?) {
    sharedText = intent?.getStringExtra(Intent.EXTRA_TEXT)
  }
}
